package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.lych.model.mongo.Friend;
import com.lych.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存是否喜欢数据
     * @param userId
     * @param likeUserId
     * @param isLike
     * @return
     */
    @Override
    public Boolean idLove(Long userId, Long likeUserId, boolean isLike) {
        try {
            //查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //查看是否有数据
            if (userLike == null) {
                //没有数据，构建保存
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //有数据，修改
                Update update = Update.update("isLike",isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询当前用户的好友详细信息
     * @param userId
     * @return
     */
    @Override
    public List<UserLike> findById(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    /**
     * 查询用户喜欢的好友id
     * @param userId
     * @return
     */
    @Override
    public List<Long> findLikeById(Long userId,Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        return likeUserId;
    }

    /**
     * 跟据id查询是否相互喜欢。
     * @param likeList
     * @param userId
     * @return
     */
    @Override
    public List<Long> fallInLove(List<Long> likeList, Long userId,Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").in(likeList).and("likeUserId").is(userId))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return CollUtil.getFieldValues(userLikes, "userId", Long.class);
    }

    /**
     * 查询谁喜欢我,得到id
     * @param userId3
     * @return
     */
    @Override
    public List<Long> likeMe(Long userId3, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId3).and("isLike").is(true))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return CollUtil.getFieldValues(userLikes, "userId", Long.class);
    }
}
